/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package IO.XML;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 *
 * @author Randy
 */
public class LoadConfiguration {
    
    XMLUtilities utilities;
    XMLUtilities utilities2;
    String[] rgbColors = {"Red","Green","Blue"};
    String[] yxPositions = {"x","y"};
    String[] speed = {"speed"};
    String[] character = {"nombre","orientacion"};
    ArrayList<Personaje> personajes;
    private boolean errorRaised = false;
    public LoadConfiguration() throws Exception, IOException{
        try {
            String basePath = new File("").getAbsolutePath();
            File xml = new File(basePath+"//Configuration.xml");
            utilities = new XMLUtilities(xml);
            utilities.Initialize();
            utilities.ParseFile();
        }
        catch (ParserConfigurationException ex) {
            Logger.getLogger(LoadConfiguration.class.getName()).log(Level.SEVERE, null, ex);
            this.errorRaised = true;
        }
        catch (SAXException ex) {
            Logger.getLogger(LoadConfiguration.class.getName()).log(Level.SEVERE, null, ex);
            this.errorRaised = true;
        }  
        if (!this.errorRaised) {
            ///load RGB
            Configuration.Configuration.Personajes = utilities.GetPersonajes();
            Configuration.Configuration.BackgroundRGB = utilities.GetConfiguration("RGB", rgbColors);
            Configuration.Configuration.InitialXY = utilities.GetConfiguration("Posicion", yxPositions);
            Configuration.Configuration.SliderDefault = utilities.GetConfiguration("speed_slider", speed);
            if (Configuration.Configuration.BackgroundRGB.isEmpty()) {
                Configuration.Configuration.BackgroundRGB = setDefaultValuesRGB();
                throw new Exception("El archivo de configuracion no es valido");
            }   
            if (Configuration.Configuration.InitialXY.isEmpty()) {
                Configuration.Configuration.InitialXY = setDefaultValuesRGB();
            }
            if (Configuration.Configuration.SliderDefault.isEmpty()) {
                Configuration.Configuration.SliderDefault = setDefaultSpeed();
            }
        } else {///if the config file is not availabe at all
            Configuration.Configuration.BackgroundRGB = setDefaultValuesRGB();
            Configuration.Configuration.InitialXY = setDefaultPosition();
            Configuration.Configuration.SliderDefault = setDefaultSpeed();
        }
    }    
    
    private Map<String, String> setDefaultValuesRGB() {
        Map<String, String> rgb = new HashMap<>(); 
            ///set RGB as black
        rgb.put("Red", "0");
        rgb.put("Green", "0");
        rgb.put("Blue", "0");
        return rgb;
    }
    
    private Map<String, String> setDefaultPosition() {
        Map<String, String> position = new HashMap<>(); 
            ///set RGB as black
        position.put("x", "0");
        position.put("y", "0");
        return position;
    }
    
    private Map<String, String> setDefaultSpeed() {
        Map<String, String> position = new HashMap<>(); 
            ///set RGB as black
        position.put("speed", "0");
        return position;
    }
}
